package com.operation;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.enetiy.Ticket;
import com.enetiy.Ticketuser;
import com.enetiy.Users;
import com.enetiy.Vipmember;
import com.service.DealService;
import com.service.TicketService;
import com.util.DateUtil;

/**
 * 查找券
 * @param ticketService
 * @throws Exception
 */
public class TicketOper {
	private String iden1 = "ticket-01";//注册券
	private String iden2 = "ticket-02";//好友推荐券
	private String iden3 = "ticket-03";//会员每日券
	private String iden4 = "ticket-04";//会员一次赠送券
	private String iden5 = "ticket-05";//充多少送多少
	
	
		public List<Ticketuser> seekTicket(TicketService ticketService,DealService dealService,  Users user ) throws Exception{
			List<Ticketuser> ticketUsers = new ArrayList<Ticketuser>();//用户可领取券
			//用户所有券
			List<Ticketuser> ticketuserList = null;
			//系统所有券
			List<Ticket> ticketList = ticketService.selectTicketList();
			Ticketuser ticketuser = null;
			
			if(!ticketList.isEmpty()){
				for (int i = 0; i <ticketList.size(); i++) {
					
					String iden = ticketList.get(i).getIden();
					//此券
					ticketuser = new Ticketuser(user, ticketList.get(i), new Date(), null,0,ticketList.get(i).getGiveTime());
					/*
					 * 1注册券 新用户既得(一次)
					 */
					if(iden.equals(iden1)){
						ticketuserList = ticketService.selectTicketuserList(user.getId(), iden1);
						if(!ticketuserList.isEmpty()){
							ticketuser = ticketuserList.get(0);
							//如用户券包已有此券  并未领取则添加至 用户可领取券集合
							if(ticketuser.getIsGet()==0){
								ticketUsers.add(ticketuser);
							}
						}else{
							//分配此券
							Serializable tid = ticketService.insertTickeUser(ticketuser);
							ticketuser.setId(Integer.parseInt(tid.toString()));
							ticketUsers.add(ticketuser);
						}
					}
					
					/*
					 * 2推荐用户券  被推荐人成为会员,推荐人既得
					 */
					if(iden.equals(iden2)){
						ticketuserList = ticketService.selectTicketuserList(user.getId(), iden2);
						if(!ticketuserList.isEmpty()){
							for (int j = 0; j < ticketuserList.size(); j++) {
								ticketuser = ticketuserList.get(i);
								if(ticketuser.getIsGet()==0){
									ticketUsers.add(ticketuser);
								}
							}
						}
					}
					/*
					 * 3会员每日登陆券  会员每日登陆既得(首次)
					 */
					boolean flg = false;
					List<Vipmember> vip = dealService.selectVipmember(user.getId());
					if(!vip.isEmpty()){
						for (int j = 0; j < vip.size(); j++) {
							long time = DateUtil.getDate(vip.get(j).getSetTime(), 5, 1).getTime();
							long time2 = new Date().getTime();
							if(vip.get(j).getWayId()==20&&time<time2){//为模式20并且时间大于第二天
								flg=true;
							}
						}
					}
					if(iden.equals(iden3)&&flg){
						ticketuserList = ticketService.selectTicketuserList(user.getId(), iden3);
						if(!ticketuserList.isEmpty()){
							Date allotTime = ticketuserList.get(ticketuserList.size()-1).getAllotTime();
							
							if(!DateUtil.isSameDay(new Date(), allotTime)){//不是同一天
								//分配此券 并添加至 用户可领取券集合
								Serializable tid = ticketService.insertTickeUser(ticketuser);
								ticketuser.setId(Integer.parseInt(tid.toString()));
								ticketUsers.add(ticketuser);
								
							}else{//是同一天
								//未领取 添加至 用户可领取券集合
								if(ticketuserList.get(ticketuserList.size()-1).getIsGet()==0){
									ticketuser = ticketuserList.get(ticketuserList.size()-1);
									ticketUsers.add(ticketuser);
								}
							}
						}else{
							//分配此券 并添加至 用户可领取券集合
							Serializable tid = ticketService.insertTickeUser(ticketuser);
							ticketuser.setId(Integer.parseInt(tid.toString()));
							ticketUsers.add(ticketuser);
						}
					}
					
					if(iden.equals(iden4)){
						ticketuserList = ticketService.selectTicketuserList(user.getId(), iden4);
						if(!ticketuserList.isEmpty()){
								ticketuser = ticketuserList.get(0);
								if(ticketuser.getIsGet()==0){
									ticketUsers.add(ticketuser);
							}
						}
					}
					
					if(iden.equals(iden5)){
						ticketuserList = ticketService.selectTicketuserList(user.getId(), iden5);
						if(!ticketuserList.isEmpty()){
							for (int j = 0; j < ticketuserList.size(); j++) {
								ticketuser = ticketuserList.get(j);
								if(ticketuser.getIsGet()==0){
									ticketUsers.add(ticketuser);
								}
							}
						}
					}
					
					
				}	
			}
			return ticketUsers;
		}
}
